# -*- coding: utf-8 -*-

__author__ = 'Leon'

from flask import g,current_app
import  sqlite3

#方法
def connect_db():
    return sqlite3.connect(current_app.config['DATA_PATH'])

#查询辅助方法
def query_db(query, args=(), one=False):
    cur = g.db.execute(query, args)
    rv = [dict((cur.description[idx][0], value)
        for idx, value in enumerate(row)) for row in cur.fetchall()]
    return (rv[0] if rv else None) if one else rv

#
def query_db1(db,query,args=(),one=False):
    cur = db.execute(query, args)
    rv = [dict((cur.description[idx][0], value)
        for idx, value in enumerate(row)) for row in cur.fetchall()]
    return (rv[0] if rv else None) if one else rv


#执行sql 插入或更新或删除
def exdb(query, args=()):
    g.db.execute(query, args)
    g.db.commit()